using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GoldenAgeHelper.common;
using GoldenAgeHelper.db;
using Fycayy.Common;
using GoldenAgeHelper.DAL;

namespace GoldenAgeHelper
{
    public partial class frmSearch : Form
    {
        public frmSearch()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string where = "1=1";
            if (this.cboFindCity.SelectedItem != null)
            {
                where = "a.cid='" + (this.cboFindCity.SelectedItem as CboItem).Key + "'";
            }
            else if (!string.IsNullOrEmpty(this.cboFindCity.Text))
            {
                where = "(a.cname in ('" + this.cboFindCity.Text.Replace(",", "','") + "') or c.huowuname in ('" + this.cboFindCity.Text.Replace(",", "','") + "'))";
            }
            DataSet ds = Database.helper.ExecuteQuery("select a.cid,a.cname,c.huowuname,b.price,b.num from city a,cityhuowu b,huowu c where a.cid=b.cid and b.hid=c.hid and " + where);
            this.dataGridView1.DataSource = ds.Tables[0];
        }

        private void btnShowMap_Click(object sender, EventArgs e)
        {
            frmMap map = new frmMap();
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                int id = Convert.ToInt32(row.Cells["colcid"].Value);
                if (id != 0 && Convert.ToInt32(row.Cells["colnum"].Value) > 0)
                {
                    map.AddCity(id);
                }
                else if (Convert.ToInt32(row.Cells["colnum"].Value) < 0)
                {
                    map.AddCityNone(id);
                }
            }
            map.Show();
        }

        IniFiles sys;
        private void frmSearch_Load(object sender, EventArgs e)
        {

            sys = new IniFiles("config/sys.ini");
            Database.Connect(DBType.Access, @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sys.ReadString("db", "src", "citis.mdb") + ";");
            Dictionary<int, string> citis = City.GetAllCity();
            this.Invoke(new GoldenAgeHelper.GA.DelInvoke(delegate()
            {

                foreach (int s in citis.Keys)
                {
                    CboItem item = new CboItem();
                    item.Key = s;
                    item.Value = citis[s];

                    this.cboFindCity.Items.Add(item);
                }

            }));
        }
    }
}